package com.weijian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weijian.dal.entity.Route;
import com.weijian.dal.enums.RouteType;
import com.weijian.mapper.RouteMapper;
import com.weijian.service.RouteService;
import com.weijian.dto.data.RouteUtilizationStats;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 路线服务实现类
 */
@Service
public class RouteServiceImpl extends ServiceImpl<RouteMapper, Route> implements RouteService {
    
    @Override
    public List<Route> getRoutesByType(RouteType routeType) {
        return this.lambdaQuery()
                .eq(Route::getRouteType, routeType)
                .list();
    }
    
    @Override
    public Route getRouteById(Integer id) {
        return this.getById(id);
    }
    
    @Override
    public List<Route> getRoutesByDataId(Integer dataId) {
        return this.lambdaQuery()
                .eq(Route::getDataId, dataId)
                .list();
    }
    
    @Override
    public List<RouteUtilizationStats> getRouteUtilizationStatsByDataId(Integer dataId) {
        // 查询指定dataId的所有路线
        List<Route> routes = this.lambdaQuery()
                .eq(Route::getDataId, dataId)
                .list();
        
        // 按路线类型分组并计算统计数据
        Map<RouteType, List<Route>> routesByType = routes.stream()
                .collect(Collectors.groupingBy(Route::getRouteType));
        
        // 转换为统计数据列表
        return routesByType.entrySet().stream().map(entry -> {
            RouteType routeType = entry.getKey();
            List<Route> typeRoutes = entry.getValue();
            
            RouteUtilizationStats stats = new RouteUtilizationStats();
            stats.setTypeCode(routeType.getCode());
            stats.setTypeDescription(routeType.getDescription());
            stats.setRouteCount((long) typeRoutes.size());
            
            // 计算总容量和已使用容量
            long totalCapacity = typeRoutes.stream()
                    .mapToLong(route -> route.getCapacity() != null ? route.getCapacity() : 0)
                    .sum();
            
            long usedCapacity = typeRoutes.stream()
                    .mapToLong(route -> {
                        if (route.getCapacity() != null && route.getRemainingCapacity() != null) {
                            return route.getCapacity() - route.getRemainingCapacity();
                        }
                        return 0;
                    })
                    .sum();
            
            stats.setTotalCapacity(totalCapacity);
            stats.setUsedCapacity(usedCapacity);
            
            // 计算平均利用率
            if (totalCapacity > 0) {
                stats.setAverageUtilizationRate((double) usedCapacity / totalCapacity);
            } else {
                stats.setAverageUtilizationRate(0.0);
            }
            
            return stats;
        }).collect(Collectors.toList());
    }
}